﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.OpenGl;
using PocketEngine.Lights;
using PocketEngine.Materials;
using PocketEngine.Maths;
namespace PocketEngine.Effects
{
    public class pEffectPointLight : pEffect
    {
        private pEffectParameter LightPos;
        private pEffectParameter LightDiff;
        private pEffectParameter LightSpec;
        private pEffectParameter LightAmb;
        private pEffectParameter MatDiff;
        private pEffectParameter MatSpec;
        private pEffectParameter MatAmb;
      
        private pEffectParameter LightRange;
        private pEffectParameter DiffMap;
        private pEffectParameter SpecMap;
        private pEffectParameter NormMap;
        private pEffectParameter ShadMap;
        private pEffectParameter CamPos;
        private pEffectParameter DiffOn;
        private pEffectParameter SpecOn;
        private pEffectParameter NormOn;
        private pEffectParameter ShadOn;
       
        public pEffectPointLight() : base(new pVertexShader("Content/Shaders/BasicMaterialVert.glsl"),new pPixelShader("Content/Shaders/BasicMaterialFrag.glsl"))
        {
      
          //  Bind(new pMaterial());
            //Gl.glUseProgram(Program);

            LightPos = GetParameter("pLightPosition");
            LightDiff = GetParameter("pLightDiffuse");
            LightSpec = GetParameter("pLightSpecular");
            LightAmb = GetParameter("pLightAmbient");
            LightRange = GetParameter("pLightRange");
            MatDiff = GetParameter("pMaterialDiffuse");
            MatSpec = GetParameter("pMaterialSpecular");
            MatAmb = GetParameter("pMaterialAmbient");
            CamPos = GetParameter("pCameraPosition");
            DiffMap = GetParameter("pDiffuseMap");
            SpecMap = GetParameter("pSpecularMap");
            NormMap = GetParameter("pNormalMap");
            ShadMap = GetParameter("pShadowMap");
            DiffOn = GetParameter("pDiffuseMapping");
            SpecOn = GetParameter("pSpecularMapping");
            NormOn = GetParameter("pNormalMapping");
            ShadOn = GetParameter("pShadowMapping");
          
            Unbind(new pMaterial());

        }

        public override void Bind(pMaterial mat)
        {
            base.Bind(mat);
            LightPos.SetVec3(pLight.Active.Position);
            LightDiff.SetCol(pLight.Active.DiffuseColor);
            LightSpec.SetCol(pLight.Active.SpecularColor);
            LightRange.SetFloat(pLight.Active.LightRange);
            LightAmb.SetCol(pLight.AmbientColor);
            MatDiff.SetCol(mat.DiffuseColor);
            MatSpec.SetCol(mat.SpecularColor);
            MatAmb.SetCol(mat.AmbientColor);
            DiffMap.SetInt(0);
            NormMap.SetInt(1);
            SpecMap.SetInt(2);
            ShadMap.SetInt(3);
            if (mat.DiffuseMapping)
            {
                DiffOn.SetInt(1);
            }
            else
            {
                DiffOn.SetInt(0);
            }
            if (mat.NormalMapping)
            {
                NormOn.SetInt(1);
            }
            else
            {
                NormOn.SetInt(0);
            }
            if (mat.SpecularMapping)
            {
                SpecOn.SetInt(1);
            }
            else
            {
                SpecOn.SetInt(0);
            }
            if (mat.ShadowMapping)
            {
                ShadOn.SetInt(1);
            }
            else
            {
                ShadOn.SetInt(0);
            }
            CamPos.SetVec3(new pVec3(Cameras.pCam.Active.ViewX+PocketEngine.Engine.pEngine.Screen.Info.Width/2,Cameras.pCam.Active.ViewY+Engine.pEngine.Screen.Info.Height/2,25*Cameras.pCam.Active.ViewScale));

        }
        public override void Unbind(pMaterial mat)
        {
            base.Unbind(mat);
        }
    }
}
